﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;

namespace Evan.Common
{
    /// <summary>
    /// 邮件帮助类
    /// </summary>
    public class EmailHelper
    {
        /// <summary>
        /// 发送邮件 此方法不可使用SSL发送  ,  只能用作普通邮件发送 使用25端口进行发送
        /// 若使用万网主机或阿里云服务器发送邮件功能用不了的问题。(请提交申请解封25端口)
        /// https://helpcdn.aliyun.com/knowledge_detail/56130.html
        /// 本方法使用 System.Net.Mail;
        /// </summary>
        /// <param name="EmailModel">sysModel中的实体类</param>
        /// <returns></returns>
        public static bool SendEmail(Model.SysModel.EmailModel EmailModel)
        {
            bool isSend = false;
            if (string.IsNullOrEmpty(EmailModel.Body) || string.IsNullOrEmpty(EmailModel.UserEmail) || string.IsNullOrEmpty(EmailModel.FromUser) || string.IsNullOrEmpty(EmailModel.UserPwd) || string.IsNullOrEmpty(EmailModel.ToUser))
                return false;

            isSend = SendMail(EmailModel.FromUser, EmailModel.ToUser, EmailModel.CcUser, EmailModel.Subject, EmailModel.Body, EmailModel.StmpServer, EmailModel.UserEmail, EmailModel.UserPwd, EmailModel.UserNick, EmailModel.IsHtml, EmailModel.FilePath, EmailModel.IsSSL, EmailModel.Port);
            return isSend;
        }

        /// <summary>
        /// 发送邮件 可作为SSL发送 
        /// 本方法使用 System.Web.Mail;
        /// </summary>
        /// <param name="EmailModel"></param>
        /// <returns></returns>
        public static bool SendEmail2(Evan.Model.SysModel.EmailModel EmailModel)
        {
            bool isSend = false;
            if (string.IsNullOrEmpty(EmailModel.Body) || string.IsNullOrEmpty(EmailModel.UserEmail) || string.IsNullOrEmpty(EmailModel.FromUser) || string.IsNullOrEmpty(EmailModel.UserPwd) || string.IsNullOrEmpty(EmailModel.ToUser))
                return false;

            isSend = SendMail2(EmailModel.FromUser, EmailModel.ToUser, EmailModel.CcUser, EmailModel.Subject, EmailModel.Body, EmailModel.StmpServer, EmailModel.UserEmail, EmailModel.UserPwd, EmailModel.UserNick, EmailModel.IsHtml, EmailModel.FilePath, EmailModel.IsSSL, EmailModel.Port);
            return isSend;
        }

        private static bool SendMail(string FromUser, string ToUser, string CcUser, string Subject, string Body, string SmtpServer, string UserName, string PassWord, string NickName, bool IsHtml, string FilePath, bool IsSSL, int Port)
        {

            try
            {

                SmtpClient _smtpClient = new SmtpClient(); //smtp验证类
                _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
                _smtpClient.Host = SmtpServer;//指定SMTP服务器
                _smtpClient.EnableSsl = IsSSL; // 如果使用SSL，则需要设置为true 
                _smtpClient.Port = Port; // SSL 使用 465 和 587 端口 
                _smtpClient.Credentials = new System.Net.NetworkCredential(UserName, PassWord);

                #region MailMessage 对象

                MailMessage _mailMessage = new MailMessage();

                MailAddress MailFrom = new MailAddress(FromUser, NickName);
                _mailMessage.From = MailFrom;

                #region 收件人集合
                if (!string.IsNullOrEmpty(ToUser))
                {
                    string[] ToUserList = ToUser.Split(',');
                    for (int i = 0; i < ToUserList.Length; i++)
                    {
                        _mailMessage.To.Add(ToUserList[i]);
                    }
                }
                #endregion

                #region 抄送集合
                if (!string.IsNullOrEmpty(CcUser))
                {
                    string[] CcUserList = CcUser.Split(',');
                    for (int i = 0; i < CcUserList.Length; i++)
                    {
                        _mailMessage.CC.Add(CcUserList[i]);
                    }
                }
                #endregion

                _mailMessage.Subject = Subject;//主题
                _mailMessage.Body = Body;//内容
                _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
                _mailMessage.IsBodyHtml = IsHtml;//设置为HTML格式
                _mailMessage.Priority = MailPriority.High;//优先级

                #region 添加附件
                if (!string.IsNullOrEmpty(FilePath))
                {
                    string[] FilePathList = FilePath.Split(',');
                    for (int i = 0; i < FilePathList.Length; i++)
                    {
                        _mailMessage.Attachments.Add(new Attachment(FilePathList[0]));
                    }
                }
                #endregion

                #endregion

                _smtpClient.Send(_mailMessage); //发送邮件

                return true;
            }
            catch (Exception ex)
            {
                
                return false;
            }
        }


        private static bool SendMail2(string FromUser, string ToUser, string CcUser, string Subject, string Body, string SmtpServer, string UserName, string PassWord, string NickName, bool IsHtml, string FilePath, bool IsSSL, int Port)
        {
            try
            {

                #region MailMessage 对象

                System.Web.Mail.MailMessage _mailMessage = new System.Web.Mail.MailMessage();

                //MailAddress MailFrom = new MailAddress(FromUser, NickName);
                _mailMessage.From = FromUser;


                #region 收件人集合
                if (!string.IsNullOrEmpty(ToUser))
                {
                    string[] ToUserList = ToUser.Split(',');
                    for (int i = 0; i < ToUserList.Length; i++)
                    {
                        _mailMessage.To = _mailMessage.To + ToUserList[i] + ";";
                    }
                }
                #endregion

                #region 抄送集合
                if (!string.IsNullOrEmpty(CcUser))
                {
                    string[] CcUserList = CcUser.Split(',');
                    for (int i = 0; i < CcUserList.Length; i++)
                    {
                        _mailMessage.Cc = _mailMessage.Cc + CcUserList[i] + ";";
                    }
                }
                #endregion

                _mailMessage.Subject = Subject;//主题
                _mailMessage.Body = Body;//内容
                _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
#pragma warning disable CS0618 // 类型或成员已过时
                _mailMessage.BodyFormat = IsHtml ? System.Web.Mail.MailFormat.Html : System.Web.Mail.MailFormat.Text;//设置为HTML格式
#pragma warning restore CS0618 // 类型或成员已过时
#pragma warning disable CS0618 // 类型或成员已过时
                _mailMessage.Priority = System.Web.Mail.MailPriority.High;//优先级
#pragma warning restore CS0618 // 类型或成员已过时

                #region 添加附件
                if (!string.IsNullOrEmpty(FilePath))
                {
                    string[] FilePathList = FilePath.Split(',');
                    for (int i = 0; i < FilePathList.Length; i++)
                    {
                        _mailMessage.Attachments.Add(new Attachment(FilePathList[0]));
                    }
                }
                #endregion

                #endregion

                //	要使用的验证类型。 1=基本验证 0=不验证 2=进程帐户(NTLM)
                _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                //用户名
                _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", UserName);
                //密码
                _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", PassWord);
                //端口 
                _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", Port);
                //使用SSL 
                _mailMessage.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", IsSSL ? "true" : "false");

                //Smtp服务器
#pragma warning disable CS0618 // 类型或成员已过时
                System.Web.Mail.SmtpMail.SmtpServer = SmtpServer;
#pragma warning restore CS0618 // 类型或成员已过时
#pragma warning disable CS0618 // 类型或成员已过时
                System.Web.Mail.SmtpMail.Send(_mailMessage);
#pragma warning restore CS0618 // 类型或成员已过时
                return true;
            }
            catch (Exception ex)
            {
               
                return false;
            }
        }


    }
}
